package com.kordar.admin.modular.sys.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kordar.admin.enums.ManagerStatus;
import com.kordar.admin.modular.sys.entity.DictEntity;
import com.kordar.admin.modular.sys.entity.DictTypeEntity;
import com.kordar.admin.modular.sys.mapper.DictMapper;
import com.kordar.admin.modular.sys.mapper.DictTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@DS("#kordardb")
public class DictServiceImpl extends ServiceImpl<DictMapper, DictEntity> implements DictService {

    DictMapper dictMapper;

    final
    DictTypeMapper dictTypeMapper;

    public DictServiceImpl(DictTypeMapper dictTypeMapper) {
        this.dictTypeMapper = dictTypeMapper;
    }

    @Autowired
    public void setMenuMapper(DictMapper dictMapper) {
        this.dictMapper = dictMapper;
    }

    @Override
    public Map<String, String> config(String code) {
        DictTypeEntity dictTypeEntity = dictTypeMapper.selectOne(new QueryWrapper<DictTypeEntity>().eq("code", code));
        if (dictTypeEntity == null) {
            return new HashMap<>();
        }

        List<DictEntity> list = list(new QueryWrapper<DictEntity>()
                .eq("dict_type_id", dictTypeEntity.getDictTypeId())
                .eq("status", ManagerStatus.OK.getCode())
        );
        return list.stream().collect(Collectors.toMap(DictEntity::getCode, DictEntity::getDescription));
    }
}
